package reflex;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Stream;
import net.sf.cglib.core.ReflectUtils;

/**
 * @author 庄子斌
 * @since 2021-03-04
 */
public class ClassUtil {

  public static void getClassMsg(Object obj)
      throws InvocationTargetException, IllegalAccessException {
    
    Class c = obj.getClass();

    Field[] fields = c.getDeclaredFields();
    for (Field fie : fields) {
      // 1.1 获取对象属性
      System.out.println(fie.getName());
      if (fie.getAnnotation(Deprecated.class) != null) {
        // 1.4 获取被 @Deprecated 注解修饰的属性
        System.out.println("被注解 @Deprecated 修饰的字段是 " + fie.getName());
      }
    }
    // 1.2  1.3   可读方法和可写方法
    readOrWriteMethod(obj, "username");
  }

  public static void readOrWriteMethod(Object c, String arg)
      throws InvocationTargetException, IllegalAccessException {
    Method[] methods = c.getClass().getDeclaredMethods();
    ArrayList<String> readList = new ArrayList<>();
    ArrayList<String> writeList = new ArrayList<>();
    for (Method method : methods) {
      if (method.getName().contains("get")) {
        if (method.getName().toLowerCase().contains(arg.toLowerCase())) {
          System.out.println("use对象的username值：" + method.invoke(c));
        }
        readList.add(method.getName());
      }
      if (method.getName().contains("set")) {
        if (method.getName().toLowerCase().contains(arg.toLowerCase())) {
          method.invoke(c, "李四");
          System.out.println("use对象的username值被修改为：" + c);
        }
        writeList.add(method.getName());
      }
    }
    
    System.out.println("可读方法集合：" + readList);
    System.out.println("可写方法集合：" + writeList);
  }
}
